<template>
  <div class="app-container">
    <Sidebar
      :conversations="conversations"
      @select="handleSelectConversation"
      @new-chat="handleNewChat"
    />
    <div class="home-content">
      <div class="welcome-card">
        <h1>问答系统</h1>
        <p class="subtitle">我是AI助手小琳，可以帮你解决IT、医疗和人事行业的问题</p>
        <div class="features">
          <div class="feature-item">
            <div class="feature-icon">💻</div>
            <div class="feature-text">IT技术咨询</div>
          </div>
          <div class="feature-item">
            <div class="feature-icon">🏥</div>
            <div class="feature-text">医疗健康建议</div>
          </div>
          <div class="feature-item">
            <div class="feature-icon">👔</div>
            <div class="feature-text">人事政策解答</div>
          </div>
        </div>
      </div>
      <ChatInput @send="handleFirstQuestion" />
    </div>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import Sidebar from '../AI/Sidebar.vue'
import ChatInput from '../AI/ChatInput.vue'
import useApi from '../AI/useApi'

export default {
  components: { Sidebar, ChatInput },
  setup() {
    const router = useRouter()
    const api = useApi()
    const conversations = ref([])
    const isLoading = ref(false)

    const fetchConversations = async () => {
      try {
        const data = await api.getAllConversations(localStorage.getItem('userid'))
        conversations.value = Array.isArray(data) ? data : []
      } catch (error) {
        console.error('加载会话列表失败:', error)
        conversations.value = []
      }
    }

    const handleSelectConversation = (id) => {
      router.push({ name: 'chat', params: { id } })
    }

    const handleNewChat = () => {
      // 已经在首页，无需处理
    }

    const handleFirstQuestion = async (question) => {
      isLoading.value = true
      try {
        const newConversation = await api.addConversation(localStorage.getItem('userid'))
        const answer = await api.getAnswer(newConversation.id, question)
        await api.updateConversationName(newConversation.id, question)

        conversations.value.unshift({ ...newConversation, name: question })
        router.push({ name: 'chat', params: { id: newConversation.id } })
      } catch (error) {
        console.error('处理问题失败:', error)
      } finally {
        isLoading.value = false
      }
    }

    onMounted(fetchConversations)

    return {
      conversations,
      isLoading,
      handleSelectConversation,
      handleNewChat,
      handleFirstQuestion
    }
  }
}
</script>

<style scoped>
.app-container {
  display: flex;
  height: 100vh;
  background-color: #f5f5f5;
}

.home-content {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  background-color: #fafafa;
}

.welcome-card {
  max-width: 600px;
  margin-bottom: 2rem;
  padding: 2rem;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
  text-align: center;
}

.welcome-card h1 {
  font-size: 2rem;
  margin-bottom: 1rem;
  color: #1890ff;
}

.subtitle {
  font-size: 1.1rem;
  color: #666;
  line-height: 1.6;
  margin-bottom: 2rem;
}

.features {
  display: flex;
  justify-content: space-around;
  margin-top: 2rem;
}

.feature-item {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.feature-icon {
  font-size: 2rem;
  margin-bottom: 0.5rem;
}

.feature-text {
  color: #666;
  font-size: 0.9rem;
}
</style>